package jeecg.business.controller.send;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import jeecg.system.pojo.base.TSDepart;
import jeecg.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import jeecg.business.entity.send.SendMessEnterEntity;
import jeecg.business.service.send.SendMessEnterServiceI;

/**   
 * @Title: Controller
 * @Description: 推送企业
 * @author LinYingPiaoJian
 * @date 2015-07-22 16:48:45
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/sendMessEnterController")
public class SendMessEnterController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SendMessEnterController.class);

	@Autowired
	private SendMessEnterServiceI sendMessEnterService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 推送企业列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "sendMessEnter")
	public ModelAndView sendMessEnter(HttpServletRequest request) {
		return new ModelAndView("jeecg/business/send/sendMessEnterList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(SendMessEnterEntity sendMessEnter,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(SendMessEnterEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, sendMessEnter);
		this.sendMessEnterService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除推送企业
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(SendMessEnterEntity sendMessEnter, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		sendMessEnter = systemService.getEntity(SendMessEnterEntity.class, sendMessEnter.getId());
		message = "删除成功";
		sendMessEnterService.delete(sendMessEnter);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加推送企业
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(SendMessEnterEntity sendMessEnter, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(sendMessEnter.getId())) {
			message = "更新成功";
			SendMessEnterEntity t = sendMessEnterService.get(SendMessEnterEntity.class, sendMessEnter.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(sendMessEnter, t);
				sendMessEnterService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			sendMessEnterService.save(sendMessEnter);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}

	/**
	 * 推送企业列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(SendMessEnterEntity sendMessEnter, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(sendMessEnter.getId())) {
			sendMessEnter = sendMessEnterService.getEntity(SendMessEnterEntity.class, sendMessEnter.getId());
			req.setAttribute("sendMessEnterPage", sendMessEnter);
		}
		return new ModelAndView("jeecg/business/send/sendMessEnter");
	}
	
	@RequestMapping(params = "toAssignEnter")
	public ModelAndView toAssignEnter(String xxid , HttpServletRequest req){
		req.setAttribute("xxid",xxid);
		String [] selectids = sendMessEnterService.getSelectetId(xxid);
		req.setAttribute("selectids", ","+getSelectedIds(selectids)+",");
		return new ModelAndView("jeecg/business/enter/assignenterList");
	}
	
	String getSelectedIds(String[] selectids){
		String subjectids = "";
		if(selectids != null && selectids.length > 0){
			for(int i=0;i<selectids.length;i++){
				if(subjectids.length() == 0) subjectids = selectids[i];
				else subjectids += ","+selectids[i];
			}
		}
		return subjectids;
	}
	
	/**
	 * 添加课程
	 * 
	 * @return
	 */
	@RequestMapping(params = "assignEnter")
	@ResponseBody
	public AjaxJson assignEnter(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String xxid = request.getParameter("xxid");		
		String ids = request.getParameter("ids");
		sendMessEnterService.assignEnter(xxid, ids);
		message = "分配成功";	
		j.setMsg(message);
		String [] selectids = sendMessEnterService.getSelectetId(xxid);
		j.setObj(","+getSelectedIds(selectids)+",");
		return j;
	}
	
	/**
	 * 删除课程
	 * 
	 * @return
	 */
	@RequestMapping(params = "delEnter")
	@ResponseBody
	public AjaxJson delEnter(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String xxid = request.getParameter("xxid");		
		String ids = request.getParameter("ids");	
		sendMessEnterService.delEnter(xxid, ids);
		message = "解除成功";	
		j.setMsg(message);
		String [] selectids = sendMessEnterService.getSelectetId(xxid);
		j.setObj(","+getSelectedIds(selectids)+",");
		return j;
	}
}
